package com.itheima.controller;

import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/students")
public class StudentController {
    @Autowired
    private StudentService studentService;
    @GetMapping()
    public Result page(StudentQueryParam param){
        log.info("查询全部的学生数据");
        PageResult<Student> studentList = studentService.findAll(param);
        return Result.success(studentList);
    }
    @PostMapping
    public Result add(@RequestBody Student student){
        log.info("添加学生数据:{}",student);
        studentService.add(student);
        return Result.success();
    }
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable Integer[] ids){
        log.info("批量删除学生数据:{}",ids);
        studentService.delete(ids);
        return Result.success();
    }
    @PutMapping
    public Result update(@RequestBody Student student){
        log.info("更新学生数据:{}",student);
        studentService.update(student);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id){
        log.info("查询id为{}的学生数据",id);
        Student student = studentService.findById(id);
        return Result.success(student);
    }
    @PutMapping("/violation/{id}/{score}")
    public Result addViolation(@PathVariable Integer id,@PathVariable Short score){
        log.info("添加学生id为{}的违规分数:{}",id,score);
        studentService.addViolation(id,score);
        return Result.success();
    }
}
